<template>
    <el-form ref="form" :model="ruleForm" :rules="rules2" label-width="80px">
        <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">立即修改</el-button>
            <el-button @click="onExit">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
// import Upload from '@/components/Upload/singleImage3';

export default {
    name: 'changePassword',
    components: {
        // Upload
    },
    props: {
        user: {
            type: Object,
            default: false
        }
    },
    data() {
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                pass: '',
                checkPass: ''
            },
            rules2: {
                checkPass: [{ validator: validatePass2, trigger: 'blur' }]
            }
        };
    },
    computed: {},
    methods: {
        onSubmit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    this.updateUser();
                    this.$emit('submit');
                } else {
                    return false;
                }
            });
        },
        onExit() {
            this.$emit('close');
        },
        updateUser() {
            this.$ajax.post('/user/admin/update', {
                id: this.user.id,
                password: this.ruleForm.checkPass
            });
        }
    }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import 'src/styles/mixin.scss';
</style>

